package forum.servlet;

import forum.dao.TopicDao;
import forum.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author haomin
 * @date 2022/07/01 19:30
 **/

@WebServlet("/editor.do")
public class EditorDoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String title = req.getParameter("title");
        String sidString = req.getParameter("type");
        int sid = Integer.parseInt(sidString);
        String content = req.getParameter("editor-markdown-doc");

        User currentUser = null;
        HttpSession session = req.getSession(false);
        if (session != null) {
            currentUser = (User) session.getAttribute("currentUser");
        }

        if (currentUser == null) {
            // 说明没有登录
            resp.sendRedirect("/login.html");
            return;
        }

        TopicDao topicDao = new TopicDao();
        topicDao.insert(sid,currentUser.uID,title,content);

        // 重定向到文章详情页
        resp.sendRedirect("/detail.html?sid=" + sid);
    }
}